package com.example.image;

import java.io.File;
import java.util.Locale;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.example.image.adapter.AdapterFolder;
import com.example.image.model.FolderItem;
import com.example.image.util.ImageUtil;

public class FolderBrowse extends Activity {
	private static final String PATH = Environment
			.getExternalStorageDirectory().getPath();

	private GridView gvFolder;
	private AdapterFolder adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS); // 注意要在setContentView方法之前设置
		setContentView(R.layout.folder_browse);
		setProgressBarIndeterminateVisibility(true); // 控制显示与隐藏
		Display display = getWindowManager().getDefaultDisplay();
		int width = display.getWidth();
		gvFolder = (GridView) findViewById(R.id.gv_folder_browse);
		adapter = new AdapterFolder(this, width);
		gvFolder.setAdapter(adapter);
		gvFolder.setOnItemClickListener(listener);
		new LoadImageFolder().execute();
	}

	private OnItemClickListener listener = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Intent intent = new Intent(FolderBrowse.this, ImageBrowse.class);
			intent.putExtra("path", adapter.getItem(position).getPath());
			intent.putExtra("imagepaths", adapter.getItem(position)
					.getImagePaths());
			startActivity(intent);
		}
	};

	class LoadImageFolder extends AsyncTask<String, FolderItem, Integer> {

		@Override
		protected Integer doInBackground(String... params) {
			File file = new File(PATH);
			Log.i("",PATH);
			scanFile(file);
			return null;
		}

		/**
		 * 扫面文件
		 * 
		 * @param file
		 */
		private void scanFile(File file) {
			File[] files = file.listFiles();
			FolderItem item = new FolderItem();
			item.setPath(file.getPath());
			if (files != null) {
				for (int i = 0; i < files.length; i++) {
					File subFile = files[i];
					if (subFile.isDirectory()) {
						scanFile(subFile);
					} else {
						String name = subFile.getName();
						if (name.indexOf(".") != -1) {
							String end = name.substring(
									name.lastIndexOf(".") + 1).toUpperCase(
									Locale.CHINA);
							if (ImageUtil.getImageTypes().contains(end)) {
								item.getImagePaths().add(subFile.getPath());
							}
						}
					}
				}
			}
			if (item.getCount() > 0) {
				publishProgress(item);
			}
		}

		@Override
		protected void onPostExecute(Integer result) {
			setProgressBarIndeterminateVisibility(false); // 控制显示与隐藏
			super.onPostExecute(result);
		}

		@Override
		protected void onProgressUpdate(FolderItem... values) {
			adapter.add(values);
			super.onProgressUpdate(values);
		}

	}

}
